<?php
	require_once KLEO_LIB_DIR.'core.lib.php';

	class gallery extends gallerymodel{

		/* Call to the model Class */
		function __construct(){
			$this->index();
		}

		function load_the_gallery(&$buff){
			$this->load_styles($buff,&$buff);
			$action = $_GET['action'];
			switch($action){
				case 'picture':
					$this->tpl_set("WEB_CONTENT", $this->load_single($_GET['id']), $gbuff, $buff);
				break;
				case 'album':
					$this->tpl_set("WEB_CONTENT", $this->load_album(intval($_GET['id'])), $gbuff, $buff);
				break;
				default:
					$this->tpl_set("WEB_CONTENT", $this->load_albums(), $gbuff, $buff);
				break;

			}
			$this->compile($gbuff, $buff);
		}
		function load_albums(){
			$query = $this->query("SELECT *, COUNT(gallery) AS count FROM ".KLEO_TABLE_PREFIX."gallery_albums, ".KLEO_TABLE_PREFIX."gallery_items WHERE status = 1 GROUP BY gallery DESC");
			while($row = $this->fetch_array($query)){
				unset($return);
				$this->tpl_load('albums.tpl', $return, false, 'gallery');
				$this->tpl_set(array(
						"ALBUM_TITLE" => $row['name'],
						"ALBUM_COUNT" => $row['count'],
						"ALBUM_THUMB" => 'thumb_'.$row['file'],
						"ALBUM_DIR" => $row['name'].'/thumbs'
						), false, $return, $return);
				$albums_buff .= $return;
			}
			return $albums_buff;
		}
		function load_album($id){
			$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."gallery_items WHERE status = 1 AND gallery = $id ORDER BY id DESC");
			while($row = $this->fetch_array($query)){
				unset($return);
				$this->tpl_load('albums.tpl', $return, false, 'gallery');
				$this->tpl_set(array(
						"PICTURE_TITLE" => $row['title'],
						"PICTURE_THUMB" => 'thumb_'.$row['file'],
						"ALBUM_DIR" => $row['name'].'/thumbs'
						), false, $return, $return);
				$pictures_buff .= $return;
			}
			return $pictures_buff;
		}
		function load_styles($buff, &$buff){
			$buff = str_replace("</head>", '<link href="modules/gallery/media/style.css" rel="stylesheet" type="text/css" />
'."\n</head>", $buff);
		}


	}
?>
